This demo is based on UART polling sending and receiving FIFO interrupt mode.
This demo is based on BL706_IOT and the connection method is as follows
GPIO function GPIO pin ---------------------------------- UART0_TX <--> GPIO14 UART0_RX <--> GPIO15
- See
examples/uart/uart_echo
for the software code
#define BSP_UART_CLOCK_SOURCE ROOT_CLOCK_SOURCE_PLL_96M
#define BSP_UART_CLOCK_DIV 0
-Configure the UART
device clock source, see bsp/board/bl706_iot/clock_config.h
#define CONFIG_GPIO14_FUNC GPIO_FUN_UART0_TX
#define CONFIG_GPIO15_FUNC GPIO_FUN_UART0_RX
- Configure
UART
device multiplexing pins, seebsp/board/bl706_iot/pinmux_config.h
#define BSP_USING_UART0
#if defined(BSP_USING_UART0)
#ifndef UART0_CONFIG
#define UART0_CONFIG \
{ \
.id = 0, \
.baudrate = 2000000,\
.databits = UART_DATA_LEN_8, \
.stopbits = UART_STOP_ONE, \
.parity = UART_PAR_NONE, \
.fifo_threshold = 1, \
}
#endif
#endif
- Enable
BSP_USING_UART0
and configure theUART
device, seebsp/board/bl706_iot/peripheral_config.h
bflb_platform_init();
- In the
bflb_platform_init
function, we have registered and opened a serial port device for debugging, to provide users with a basic function ofMSG
for printing out messages. The specific implementation is as follows
uart_register(board_get_debug_uart_index(), "debug_log", DEVICE_OFLAG_RDWR);
struct device *uart = device_find("debug_log");
if (uart)
{
device_open(uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX);
device_set_callback(uart, NULL);
device_control(uart, DEVICE_CTRL_CLR_INT, (void *)(UART_RX_FIFO_IT));
}
- First call the
uart_register
function to register theUART
device, currently registerUART0
- Then use the
find
function to find the handle corresponding to the device and save it in theuart
handle - Finally use
device_open
to open theuart
device with polling sending and interrupt receiving, the interrupt is closed by default and the receiving interrupt callback function is not registered
if (uart)
{
device_set_callback(uart, uart_irq_callback);
device_control(uart, DEVICE_CTRL_SET_INT, (void *)(UART_RX_FIFO_IT|UART_RTO_IT));
}
- Register the user-specified
UART0
receiving interrupt service function through thedevice_set_callback
function. Open theRX_FIFO
andRTO
interrupts through thedevice_control
function
void uart_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
{
if (state == UART_EVENT_RX_FIFO)
{
device_write(dev,0,(uint8_t *)args,size);
}
else if (state == UART_EVENT_RTO)
{
device_write(dev,0,(uint8_t *)args,size);
}
}
This function is the interrupt service function of the example, and its function is to send out the received data.
state
will return the interrupt type of theUART
deviceargs
contains the return data pointersize
contains the length of the returned datadev
is the handle of the interrupteduart
device
When an interrupt occurs, the
device_write
function will be called to send the received data back.
CDK compilation
Open project:uart_echo.cdkproj
Refer to the steps of :ref:`windows_cdk_quick_start` to compile and download
Command compilation
$ cd <sdk_path>/bl_mcu_sdk
$ make BOARD=bl706_iot APP=uart_echo
Program
Video display: